package org.itboys.weixin.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.admin.dto.EasyUiTreeDO;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.collection.CommonCollectionUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.weixin.entity.WeixinConfig;
import org.itboys.weixin.service.WeixinConfigService;
import org.itboys.weixin.service.WeixinRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Lists;

@RestController
@RequestMapping(value = "/weixin/rel")
public class WeixinRelController extends BaseController{
	
	@Autowired
	private WeixinRelService weixinRelService;
	@Autowired
	private WeixinConfigService weixinConfigService1;
	
    @RequestMapping("/gorel")
    public ModelAndView gorel(HttpServletRequest request,Model model){
		return new ModelAndView("/weixin/rel/list");
	}
    
   ///用户关联微信
	@RequestMapping("setUserRelwx")
	public void setUserRelwx(HttpServletRequest request,HttpServletResponse response){
		try {
			Long relObjectId = Long.valueOf(request.getParameter("relObjectId"));
			String str = request.getParameter("weixinId");
			List<Long> weixinId = CommonCollectionUtils.splictToLongList(str, ",");
			weixinRelService.deleteByObjIdAndType(relObjectId, "1");
			weixinRelService.insertRel(relObjectId, weixinId,"1");
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("weixinrel setUserRelwx error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	
	@RequestMapping("/role")
    public ModelAndView role(HttpServletRequest request,Model model){
		return new ModelAndView("/weixin/rel/relrolelist");
	}
	///用户关联微信
	@RequestMapping("setRoleRelwx")
	public void setRoleRelwx(HttpServletRequest request,HttpServletResponse response){
		try {
			
			Long relObjectId = Long.valueOf(request.getParameter("relObjectId"));
			String str = request.getParameter("weixinId");
			List<Long> weixinId = CommonCollectionUtils.splictToLongList(str, ",");
			weixinRelService.deleteByObjIdAndType(relObjectId, "2");
			weixinRelService.insertRel(relObjectId, weixinId,"2");
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("weixinrel setRoleRelwx error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	
	/**
	 *获取微信号树 并全选中
	 * @param id
	 * @param response
	 */
	@RequestMapping("getWeixinConfigTree1")
	public void getWeixinConfigTree(HttpServletResponse response,@RequestParam Long userId,@RequestParam String type){
//		Long projectId=0L;
		List<Long> relWeixinIds = weixinRelService.list(userId,type);
		getWeixinConfigByProjectId(response,relWeixinIds);
	}

	private void getWeixinConfigByProjectId(HttpServletResponse response,List<Long> relWeixinIds) {
		List<WeixinConfig> modules = weixinConfigService1.getByParentIdAndProjectId(0l);//获取具有权限的模板
		List<EasyUiTreeDO> tree = Lists.newArrayListWithCapacity(modules.size());
		for(WeixinConfig am:modules){
			EasyUiTreeDO module = new EasyUiTreeDO();
			module.setId(am.getId());
			module.setText(am.getName());
			if (relWeixinIds.contains(am.getId())) {
				module.setChecked(true);
				
			}
			module.setChildren(prepareChildren(am.getId(),relWeixinIds));
			tree.add(module);
		}
		AjaxUtils.renderJson(response, tree);
	}

	public List<EasyUiTreeDO> prepareChildren(Long parentId,List<Long> relWeixinIds){
		List<EasyUiTreeDO> module=Lists.newArrayList();
		List<WeixinConfig> children =Lists.newArrayList();
		if(weixinConfigService1.getByParentIdAndProjectId(parentId).size()>0){
			children=weixinConfigService1.getByParentIdAndProjectId(parentId);
			for(WeixinConfig wc:children){
				EasyUiTreeDO permission = new EasyUiTreeDO();
				permission.setId(wc.getId());
				permission.setText(wc.getName());
				if (relWeixinIds.contains(wc.getId())) {
					permission.setChecked(true);
				}
				permission.setChildren(prepareChildren(wc.getId(),relWeixinIds));
				module.add(permission);
			}
		}
		return module;
	}
	
}
